package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.nursing.domain.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.pojo.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    // 自定义redis的键值，随便命名，但要见名知意 ： 代表的是目录
    private static final String CHECK_KEY_PREFIX="nursingLevel:all";

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        int insert = nursingLevelMapper.insert(nursingLevel);
        redisTemplate.delete(CHECK_KEY_PREFIX);
        return insert;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        int i = nursingLevelMapper.updateById(nursingLevel);
        redisTemplate.delete(CHECK_KEY_PREFIX);
        return i;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        int i = nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
        redisTemplate.delete(CHECK_KEY_PREFIX);
        return i ;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        int i = nursingLevelMapper.deleteById(id);
        redisTemplate.delete(CHECK_KEY_PREFIX);
        return i;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }


    /**
     * 查询所有护理等级
     * @return
     */
    @Override
    public List<NursingLevelVo> getAllLevelInfo() {
        // 1.首先从redis中取出来，如果redis中没有，则从数据库中查表，然后放到redis中
        ValueOperations<Object, Object> ops = redisTemplate.opsForValue();
        List<NursingLevelVo> list = (List<NursingLevelVo>) ops.get(CHECK_KEY_PREFIX);
        if(CollectionUtils.isEmpty(list)){
            List<NursingLevel> nursingLevels = this.list(Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getStatus, 1));

            list = BeanUtil.copyToList(nursingLevels, NursingLevelVo.class);

            ops.set(CHECK_KEY_PREFIX,list);
        }
        // 不执行if就直接从redis中取出来，返回
        return list;
    }
}
